package com.core.ui.customtag;

import java.io.IOException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import com.core.protocols.IDetailTableViewDelegate;

public class DetailTableView
        extends TagSupport {

	private IDetailTableViewDelegate delegate;
	private String                   userInfo;
	private String                   tableClass;



	public DetailTableView() {

		this.tableClass = "properties-list-two-columns";
	}



	public String getTableClass () {

		return tableClass;
	}



	public void setTableClass ( String tableClass ) {

		this.tableClass = tableClass;
	}



	public String getUserInfo () {

		return userInfo;
	}



	public void setUserInfo ( String userInfo ) {

		this.userInfo = userInfo;
	}



	public IDetailTableViewDelegate getDelegate () {

		return delegate;
	}



	public void setDelegate ( IDetailTableViewDelegate delegate ) {

		this.delegate = delegate;
	}



	@Override
	public int doStartTag ()
	        throws JspException {

		StringBuilder sb = new StringBuilder();

		sb.append("<div class=\"mod-content\">");
		sb.append(String.format("<ul class=\"%s\">", this.tableClass));
		if ( delegate != null ) {
			int itemsCount = delegate.detailTableViewItemsCount(userInfo);
			for ( int i = 0; i < itemsCount; i++ ) {
				String item = delegate.detailTableViewItemAtIndex(userInfo, i);
				sb.append(item);
			}
		}
		sb.append("</ul>");
		sb.append("</div>");

		try {
			JspWriter writer = pageContext.getOut();
			writer.append(sb.toString());
		} catch ( IOException e ) {
			throw new JspException(e.getMessage());
		}

		return SKIP_BODY;
	}

	/**
     * 
     */
	private static final long serialVersionUID = 1L;

}
